package com.kolon.business.mKolonInt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mKolonInt.controller.MKolonIntViewController")
public class MKolonIntViewController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mKolonInt/mKolonIntView.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String CompanyCode = (String)userMap.get("CompanyCD");
		String page = ComUtil.nullChk(request.getParameter("page"), "1");

		// URL 생성
		StringBuffer buffer = new StringBuffer();
		buffer.append(url);
		buffer.append("/kolon_int.aspx?strMode=").append("4");
		buffer.append("&ArticleCD=").append(request.getParameter("ArticleCD"));
		buffer.append("&userid=").append(id);
		buffer.append("&pw=").append(pw);
				
			
		// XML DOCUMENT 및 인증 처리
		Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren("BoardDetail");
		Iterator itXml = xmlList.iterator();
		List mapList = new ArrayList();
		Map map = new HashMap();
		// XML 데이터 
		if (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			map = new HashMap();
			map.put("ArticleCD", el.getChild("ArticleCD").getText());
			map.put("UserID", el.getChild("UserID").getText());
			map.put("UserName", el.getChild("UserName").getText());
			map.put("Title", el.getChild("Title").getText());
			map.put("Contents", el.getChild("Contents").getText().replaceAll("&lt;","<").replaceAll("&gt;",">").replaceAll("&nbsp;"," ").replaceAll("&amp;","&"));
			map.put("CreateDate", el.getChild("CreateDate").getText().substring(0, 10));
			map.put("ExpireDate", el.getChild("ExpireDate").getText());
			map.put("ViewCnt", ComUtil.addComma(el.getChild("ViewCnt").getText()));
			map.put("FileOnly", el.getChild("FileOnly").getText());
			map.put("Importance", el.getChild("Importance").getText());
			map.put("BoardName", el.getChild("BoardName").getText());
			map.put("IsNotice", el.getChild("IsNotice").getText());
			map.put("B_ACL", el.getChild("B_ACL").getText());
			map.put("BoardCD", el.getChild("BoardCD").getText());
			map.put("SAdmin", el.getChild("SAdmin").getText());
			map.put("PublicCanWrite", el.getChild("PublicCanWrite").getText());
			map.put("searchauth", el.getChild("searchauth").getText());
			map.put("Gubun", el.getChild("Gubun").getText());
			map.put("Company", el.getChild("Company").getText());
			map.put("Price", el.getChild("Price").getText());
			map.put("Area", el.getChild("Area").getText());
			map.put("Status", el.getChild("Status").getText());
			map.put("Phone", el.getChild("Phone").getText());
			map.put("Address", el.getChild("Address").getText());
			map.put("Theme", el.getChild("Theme").getText());
			map.put("Point", el.getChild("Point").getText());
			map.put("NickName", el.getChild("NickName").getText());
			map.put("Parking", el.getChild("Parking").getText());
			map.put("Ref_Seq", el.getChild("Ref_Seq").getText());			
		    map.put("UCStatus", el.getChild("UCStatus").getText());
			map.put("TelOffice", el.getChild("TelOffice").getText());
			map.put("Mobile", el.getChild("Mobile").getText());
			//mapList.add(map);
		}
		
		// 이전글				
		StringBuffer bufferP = new StringBuffer();
		bufferP.append(url);
		bufferP.append("/kolon_int.aspx?strMode=prenext");
		//bufferP.append("&BoardRootCD=").append(request.getParameter("strBoardRootCD"));
		bufferP.append("&ArticleCD=").append(request.getParameter("ArticleCD"));
		bufferP.append("&param=P");
		//bufferP.append("&BoardCD=").append(request.getParameter("BoardCD"));
		//bufferP.append("&SortFieldName=SortCreateDate");
		//bufferP.append("&SortAscDesc=desc");		
		//bufferP.append("&CompanyCode=").append(CompanyCode);
		bufferP.append("&userid=").append(id);
		bufferP.append("&pw=").append(pw);
					
		String previous = ComUtil.getDocumentText(bufferP.toString(), id, pw);
		map.put("previous", previous);
		
		
		// 다음글				
		StringBuffer bufferN = new StringBuffer();
		bufferN.append(url);
		bufferN.append("/kolon_int.aspx?strMode=prenext");
		//bufferN.append("&BoardRootCD=").append(request.getParameter("strBoardRootCD"));
		bufferN.append("&ArticleCD=").append(request.getParameter("ArticleCD"));
		bufferN.append("&param=N");
		//bufferN.append("&BoardCD=").append(request.getParameter("BoardCD"));
		//bufferN.append("&SortFieldName=SortCreateDate");
		//bufferN.append("&SortAscDesc=desc");		
		//bufferN.append("&CompanyCode=").append(CompanyCode);
		bufferN.append("&userid=").append(id);
		bufferN.append("&pw=").append(pw);
					
		String next = ComUtil.getDocumentText(bufferN.toString(), id, pw);
		map.put("next", next);
		
		

		// UserInfo 영역
		Document doc2 = ComUtil.getDocument(buffer.toString(), id, pw);
		Element element2 = doc2.getRootElement();
		List xmlList2 = element2.getChildren("UserInfo");
		Iterator itXml2 = xmlList2.iterator();
		List mapList2 = new ArrayList();
		Map map2 = new HashMap();
		// XML 데이터 
		if (itXml2.hasNext()) {
			Element el = (Element)itXml2.next();
			map2 = new HashMap();
			map2.put("UserID", el.getChild("UserID").getText());
			//mapList2.add(map);
		}

		
		// BoardComment 영역
		Document doc3 = ComUtil.getDocument(buffer.toString(), id, pw);
		Element element3 = doc3.getRootElement();
		List xmlList3 = element3.getChildren("BoardComment");
		Iterator itXml3 = xmlList3.iterator();
		List mapList3 = new ArrayList();
		Map map3 = new HashMap();
		// XML 데이터 
		if (itXml3.hasNext()) {
			Element el = (Element)itXml3.next();
			map3 = new HashMap();
			map3.put("CommentCD", el.getChild("CommentCD").getText());
			map3.put("UserID", el.getChild("UserID").getText());
			map3.put("UserName", el.getChild("UserName").getText());
			map3.put("Contents", el.getChild("Contents").getText());
			map3.put("CreateDate", el.getChild("CreateDate").getText().substring(0, 10));
			map3.put("EmoID", el.getChild("EmoID").getText());
			map3.put("Url", el.getChild("Url").getText());
			map3.put("ArticleCD", el.getChild("ArticleCD").getText());
			map3.put("UCStatus", el.getChild("UCStatus").getText());
			map3.put("TelOffice", el.getChild("TelOffice").getText());
			map3.put("Mobile", el.getChild("Mobile").getText());
			mapList3.add(map3);
		}
		
		
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("viewMap", map);
		modelAndView.addObject("userMap", map2);
		modelAndView.addObject("mapList3", map3);
		
		modelAndView.addObject("strMode", request.getParameter("strMode")==null ? "18" : request.getParameter("strMode"));
		modelAndView.addObject("BoardCD", request.getParameter("BoardCD"));
		modelAndView.addObject("Page", request.getParameter("Page"));
		modelAndView.addObject("B_IsSub", request.getParameter("B_IsSub"));
		modelAndView.addObject("Depth", request.getParameter("Depth"));
		modelAndView.addObject("page", page);
		
		modelAndView.addObject("SearchTitle", request.getParameter("SearchTitle"));
		modelAndView.addObject("SearchStartDate", request.getParameter("SearchStartDate"));
		modelAndView.addObject("SearchEndDate", request.getParameter("SearchEndDate"));
		String edtChk = "N";
		// 글내용 중 태그 존재시 (PC에서 에디터를 활용해서 등록한 경우 체크)
		if (((String)map.get("Contents")).indexOf("<html>") != -1) {
			edtChk = "Y";
		}
		modelAndView.addObject("edtChk", edtChk);
		return modelAndView;
	}

}